package com.dou.you.che.web.controller.sys;

import com.alibaba.fastjson.JSON;
import com.dou.you.che.comm.utils.constant.ConfigConstant;
import com.dou.you.che.comm.utils.page.PageUtils;
import com.dou.you.che.comm.utils.query.QueryParam;
import com.dou.you.che.comm.utils.response.RspData;
import com.dou.you.che.core.entity.conf.SmsConfig;
import com.dou.you.che.core.entity.sys.SysSmsLog;
import com.dou.you.che.service.sys.SysConfigServiceI;
import com.dou.you.che.service.sys.SysSmsLogServiceI;
import com.dou.you.che.web.controller.base.BaseAct;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 短消息
 * @Author 黄国刚【1058118328@qq.com】
 */
@RestController
@RequestMapping("/sys/smslog")
public class SysSmsLogAct extends BaseAct{

    @Autowired
    private SysSmsLogServiceI smsLogService;
    @Autowired
    private SysConfigServiceI sysConfigService;

    private final static String KEY = ConfigConstant.SMS_CONFIG_KEY;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:smslog:list")
    @ResponseBody
    public RspData list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        QueryParam query = new QueryParam(params);

        List<SysSmsLog> smsLogList = smsLogService.queryList(query);
        int total = smsLogService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(smsLogList, total, query.getLimit(), query.getPage());

        return RspData.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:smslog:info")
    @ResponseBody
    public RspData info(@PathVariable("id") String id) {
        SysSmsLog smsLog = smsLogService.queryObject(id);

        return RspData.ok().put("smsLog", smsLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:smslog:save")
    @ResponseBody
    public RspData save(@RequestBody SysSmsLog smsLog) {

        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        smsLogService.save(smsLog);

        return RspData.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:smslog:update")
    @ResponseBody
    public RspData update(@RequestBody SysSmsLog smsLog) {

        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        smsLogService.update(smsLog);

        return RspData.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:smslog:delete")
    @ResponseBody
    public RspData delete(@RequestBody String[] ids) {

        if(isTest){
            return RspData.error("演示模式不允许操作");
        }

        smsLogService.deleteBatch(ids);

        return RspData.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public RspData queryAll(@RequestParam Map<String, Object> params) {

        List<SysSmsLog> list = smsLogService.queryList(params);

        return RspData.ok().put("list", list);
    }

    /**
     * 短信配置信息
     */
    @RequestMapping("/config")
    public RspData config() {
        SmsConfig config = sysConfigService.getConfigObject(KEY, SmsConfig.class);

        return RspData.ok().put("config", config);
    }

    /**
     * 保存短信配置信息
     */
    @RequestMapping("/saveConfig")
    public RspData saveConfig(@RequestBody SmsConfig config) {

        if(isTest){
            return RspData.error("演示模式不允许操作");
        }

        sysConfigService.updateValueByKey(KEY, JSON.toJSONString(config));
        return RspData.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/sendSms")
    @ResponseBody
    public RspData sendSms(@RequestBody SysSmsLog smsLog) {
        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        SysSmsLog SysSmsLog = smsLogService.sendSms(smsLog);
        return RspData.ok().put("result", SysSmsLog);
    }
}
